/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package modelviewcontroller;


import java.awt.event.MouseEvent;
import java.util.Iterator;
import javax.swing.JComponent;
import layouteditor.view.ElementRightClickMenu;
import layouteditor.view.InsertOnRightClickMenu;
import layoutitems.CurrentLayoutElements;
import layoutitems.ImageElement;

/**
 *
 * @author ST
 */
public class MouseController {

    //#######################
    //      Fields
    //#######################
    private static ElementRightClickMenu elementRightClickMenu;
    private static InsertOnRightClickMenu insertOnRightClickMenu;
    //#######################
    //      Constructor
    //#######################

    public MouseController(JComponent jFrame) {
        elementRightClickMenu = new ElementRightClickMenu(jFrame);
        insertOnRightClickMenu = new InsertOnRightClickMenu(jFrame);
    }

    //#######################
    //      Inner Methods   
    //#######################
    public void onLeftMouseButtonClicked(MouseEvent e) {
        ImageElement makeDragable = null;
        int actDistance = 0;
        int lastDistance = Integer.MAX_VALUE;
        int[] centerXY = new int[2];
        for (Iterator<ImageElement> it = CurrentLayoutElements.allVisibleLayoutElements.iterator(); it.hasNext();) {
            ImageElement image = it.next();
            centerXY = image.getCenter();
            actDistance = (int) (Math.abs(centerXY[0] - e.getX()) + Math.abs(centerXY[1] - e.getY()));
            if (!image.getBlocked()) {
                if (actDistance < lastDistance) {
                    if (clickedIntoPicture(image, e)) {
                        makeDragable = image;
                    }
                    //System.out.println(makeDragable.getName());
                    //System.out.println(actDistance);
                    //System.out.println(makeDragable.getBlocked());
                }
                lastDistance = actDistance;
            }
        }
        if (makeDragable != null) {
            makeDragable.setMarked(true);
            makeDragable.setDragable(true);
            //if (CurrentLayoutElements.lastMarkedElement == null) {
            //    CurrentLayoutElements.lastMarkedElement = new ImageElement();
            //}
            //CurrentLayoutElements.lastMarkedElement = makeDragable;
        }
    }

    public boolean clickedIntoPicture(ImageElement makeDragable, MouseEvent e) {
        //macht Image mit geringstem Abstand bewegbar
        if (makeDragable != null) {
            //System.out.println(makeDragable.getName());
            int x = makeDragable.getCenter()[0];
            int y = makeDragable.getCenter()[1];
            int halfWidth = makeDragable.getImage().getWidth(null) / 2;
            int halfHeight = makeDragable.getImage().getWidth(null) / 2;

            //System.out.println(x + "; " + e.getX());
            //System.out.println(y + "; " + e.getY());
            if (Math.abs(x - e.getX()) <= halfWidth) {
                //System.out.println("in halfWidth ");
                if (Math.abs(y - e.getY()) <= halfHeight) {
                    return true;
                }
            }
        }
        return false;
    }

    public void onRightMouseButtonClicked(JComponent frameLayeredPane, MouseEvent e) {
        //Markiertes Layout-Element identifizieren
        int[] center = new int[2];
        ImageElement markedElement = null;
        for (Iterator<ImageElement> it = CurrentLayoutElements.allVisibleLayoutElements.iterator(); it.hasNext();) {
            ImageElement marked = it.next();
            if (marked.getMarked()) {
                center = marked.getCenter();
                markedElement = marked;
            }
        }
        if (markedElement != null) {
            elementRightClickMenu.showMenu(frameLayeredPane, markedElement, center[0], center[1]);
        }
        else if (markedElement == null){
            //int x = (int)((float)frameLayeredPane.getWidth()/2f);
            //int y = (int)((float)frameLayeredPane.getHeight()/2f);
            int x = e.getX();
            int y = e.getY();
            insertOnRightClickMenu.showMenu(frameLayeredPane, x, y);
        }
    }
}
